package com.tianya.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tianya.domain.dto.CourseRequestDTO;
import com.tianya.domain.entity.Course;
import com.tianya.domain.entity.Teacher;
import com.tianya.domain.entity.User;
import com.tianya.domain.vo.CourseVO;
import com.tianya.domain.vo.PageResultVO;
import com.tianya.mapper.TeacherMapper;
import com.tianya.mapper.UserMapper;
import com.tianya.service.CourseService;
import com.tianya.mapper.CourseMapper;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
* @author 86191
* @description 针对表【course(课程表)】的数据库操作Service实现
* @createDate 2025-05-24 18:59:06
*/
@Service
public class CourseServiceImpl extends ServiceImpl<CourseMapper, Course>
    implements CourseService{
    @Resource
    private TeacherMapper teacherMapper;

    @Resource
    private UserMapper  userMapper;
    @Override
    public void createCourse(CourseRequestDTO courseRequestDTO) {
        Course course = Course.builder()
                .name(courseRequestDTO.getName())
                .description(courseRequestDTO.getDescription())
                .teacherId(courseRequestDTO.getTeacherId())
                .price(courseRequestDTO.getPrice())
                .durationHours(courseRequestDTO.getDurationHours())
                .build();
        this.save(course);
    }

    @Override
    public PageResultVO<CourseVO> pageList(Integer pageNum, Integer pageSize, CourseRequestDTO courseRequestDTO) {
        Page pageParam = new Page<>(pageNum, pageSize);
        Page<Course> pageResult = baseMapper.selectPage(pageParam, null);
        PageResultVO<CourseVO> pageVO = new PageResultVO<>();
        pageVO.setTotal(pageResult.getTotal());
        List<CourseVO> courseVOList = new ArrayList<>();
        for (Course course : pageResult.getRecords()) {
            CourseVO courseVO = new CourseVO();
            BeanUtil.copyProperties(course, courseVO);
            courseVOList.add(courseVO);
        }
        pageVO.setRecords(courseVOList);
        return pageVO;
    }

    @Override
    public List<CourseVO> listCourses() {
        List<Course> courseList = baseMapper.selectList(null);
        List<CourseVO> courseVOList = new ArrayList<>();
        for (Course course : courseList) {
            CourseVO courseVO = new CourseVO();
            Long teacherId = course.getTeacherId();
            Teacher  teacher = teacherMapper.selectById(teacherId);
            Long userId = teacher.getUserId();
            User user = userMapper.selectById(userId);
            courseVO.setTeacherName(user!=null?user.getName():"unknown");
            courseVO.setId(course.getCourseId());
            BeanUtil.copyProperties(course, courseVO);
            courseVOList.add(courseVO);
        }
        return courseVOList;
    }


}




